﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;

namespace RecordBeRun
{
    public class UserViewModel : ViewModelBase
    {
        private BindingList<Mentor> _allMentors;
        public BindingList<Mentor> AllMentors
        {
            get
            {
                if (_allMentors == null)
                {
                    _allMentors = GetAllMentors();
                }
                return _allMentors;
            }
            set
            {
                _allMentors = value;
                NotifyPropertyChanged("AllMentors");
            }
        }

        private BindingList<Graduate> _allGraduates;
        public BindingList<Graduate> AllGraduates
        {
            get
            {
                if (_allGraduates == null)
                {
                    _allGraduates = GetAllGrduates();
                }
                return _allGraduates;
            }
            set
            {
                _allGraduates = value;
                NotifyPropertyChanged("AllGraduates");
            }
        }

        private BindingList<Graduate> _graduatesUnderMentor;
        public BindingList<Graduate> GraduatesUnderMentor
        {
            get
            {
                if (_graduatesUnderMentor == null)
                    _graduatesUnderMentor = new BindingList<Graduate>();
                GetGraduatesByMentor(SelectedMentor.Id);
                return _graduatesUnderMentor;
            }
            set
            {
                _graduatesUnderMentor = value;
                NotifyPropertyChanged("GraduatesUnderMentor");
            }
        }

        private Mentor _selectedMentor;
        public Mentor SelectedMentor
        {
            get
            {
                return _selectedMentor;
            }
            set
            {
                _selectedMentor = value;
            }
        }

        private BindingList<Mentor> GetAllMentors()
        {
            return GetAllModels<Mentor>("Data/Mentors.xml");
        }
        private BindingList<Graduate> GetAllGrduates()
        {
            return GetAllModels <Graduate>("Data/Graduates.xml");
        }
        private BindingList<T> GetAllModels<T>(string dataFileUrl)
        {
            BindingList<T> rtn = new BindingList<T>();
            var data = XMLHelper.GetModelFromXmlFile<T>(dataFileUrl);
            foreach (var item in data)
            {
                rtn.Add(item);
            }
            return rtn;
        }
        private void GetGraduatesByMentor(Guid mentorId)
        {
            _graduatesUnderMentor.Clear();
            foreach (var g in AllGraduates)
            {
                if (g.MentorId == mentorId)
                {
                    _graduatesUnderMentor.Add(g);
                }
            }
        }
    }
}
